package com.mengtian_tech.tju.dataquery.api.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

/**
 * 车辆进出
 */
@Service
public class CarParkService {
	
	@Autowired
	@Qualifier("jdbcTemplateDataAi")
	JdbcTemplate jdbcTemplateDataAi;
	

	@Autowired
	@Qualifier("jdbcTemplateCarEnter")
	JdbcTemplate jdbcTemplateCarEnter;
	
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	
	/**
	 * 同步数据
	 */
	public synchronized int asyncCarPark() {
		//查询条件
		String where = "";
		
		//首先获取最近的同步信息
		List<Map<String,Object>> asyncList = this.jdbcTemplateDataAi.queryForList("select * from async_log_car_gate where sync_count > 0   order by id desc limit 0,1");
		if(asyncList.size() > 0) {
			Map<String,Object> map = asyncList.get(0);
			if(map != null && map.get("end_id") != null) {
				where = " where id  >  " + map.get("end_id") + " ";
			}
		}
		
		
		Date start = new Date();
		int count = 0;
		String startId = null;
		String endId = null;
		Date lastCreateTime = null;
		
		List<Map<String,Object>> list =  this.jdbcTemplateCarEnter.queryForList("select * from ( select * from  sharecenter.TP_PARK_RECORD   "+ where +" order by id  asc ) where rownum <=1000 ");
		
		try {		
			for(Map<String,Object> item:list) {
				if(startId == null) startId = item.get("ID").toString();
				endId =   item.get("ID").toString();
				lastCreateTime = (Date) item.get("CREATE_TIME");
				boolean ret =  insert(item);
				if(ret) count++;
			}
			
		}catch (Exception e) {
			e.printStackTrace();
		}finally {
			Date end = new Date();
			long seconds = (end.getTime() - start.getTime())/1000;
			
			//插入数据库记录
			if(count > 0 ) {
				this.jdbcTemplateDataAi.update(
						"insert into async_log_car_gate(task_begin,task_end,sync_count,start_id,end_id,duration,last_create_time) values(?,?,?,?,?,?,?)",
						start,end,count,startId,endId,seconds,lastCreateTime);
			}
						
			
		}
		return count;
		
	}

	private boolean insert(Map<String, Object> item) {
		try {
			
			Date inTime = (Date) item.get("IN_TIME");
			String dateOn = sdf.format(inTime);
			
			
			this.jdbcTemplateDataAi.update("insert into car_park_record(CREATE_TIME,UPDATE_TIME,RECORD_ID,ORIGINAL_RECORD_IN_ID,ORIGINAL_RECORD_OUT_ID,ORIGINAL_IN_VOUCHER_TYPE,"
					+ "ORIGINAL_OUT_VOUCHER_TYPE,EQUIPMENT_IN_ID,EQUIPMENT_OUT_ID,IN_TIME,OUT_TIME,DYNAMIC_BILLING_OUT_TIME,"
					+ "MULTIPLE_CAR_TO_TEMP,VOUCHER_TYPE,VOUCHER_NO,PLATE_COLOR,IN_PHYSICAL_NO,OUT_PHYSICAL_NO,"
					+ "IN_IMG_ADDR,OUT_IMG_ADDR,STATUS,IN_RECORD_TYPE,WITHHOLD_FLAG,WITHHOLD_MODE,WITHHOLD_LIMIT,"
					+ "WITHHOLD_BALANCE,WITHHOLD_USERSTATUS,WITHHOLD_CHANNEL,IN_USER_ID,IN_USER_NAME,IN_PASS_ID,"
					+ "IN_PASS_NAME ,IN_AREA_ID,PARK_ID,PROJECT_CODE,IN_REMARK,OUT_REMARK,CAR_TYPE,"
					+ "OUT_PASS_ID,OUT_AREA_ID,OUT_USER_ID,OUT_RECORD_TYPE,OLD_VOUCHER_NO,RECORD_TYPE,IN_EVENT_TYPE,"
					+ "OUT_EVENT_TYPE,IN_MEAL_ID,OUT_MEAL_ID,WITHHOLD_USER_ID,OUT_TYPE,PARTITION_CODE,IS_LOCK,"
					+ "LOCK_RECORD_ID,SERVICE_ID,SERVICE_NAME,PERSON_ID,PERSON_NAME,person_name_cipher,PERSON_PHONE,"
					+ "person_phone_cipher,PERSON_PHONE_LAST,CAR_SERVICE_TYPE,SYNC_REMARK,SYNC_TIME,SYNC_FLAG,"
					+ "SYNC_FAILS,IS_ONLINE_CALC,ONLINECALCTYPE,ONLINECALCTYPEDES,OUTSOUNDTEXT,OUTSCREENTEXT,SOFTTEXT,"
					+ "DELETE_FLAG,PASS_MODE,DATA_ORIGIN,ORIGIN_ID,STARTTIME,ENDTIME,STARTDATE,ENDDATE,"
					+ "IN_OPEN_GATE,IN_OPEN_TIME,OUT_OPEN_GATE,OUT_OPEN_TIME,SERVICE_CHANGE_TIME,SERVICE_CHANGE_SIGN,"
					+ "IN_OPEN_ORIGIN,OUT_OPEN_ORIGIN,form_id,date_on) "
					+ " values(?,?,?,?,?,?,?,?,?,?"
					+ ",?,?,?,?,?,?,?,?,?,?"
					+ ",?,?,?,?,?,?,?,?,?,?"
					+ ",?,?,?,?,?,?,?,?,?,?"
					+ ",?,?,?,?,?,?,?,?,?,?"
					+ ",?,?,?,?,?,?,?,?,?,?"
					+ ",?,?,?,?,?,?,?,?,?,?"
					+ ",?,?,?,?,?,?,?,?,?,?"
					+ ",?,?,?,?,?,?,?,?,?,?) ",
					item.get("CREATE_TIME"),item.get("UPDATE_TIME"),item.get("RECORD_ID"),item.get("ORIGINAL_RECORD_IN_ID"),item.get("ORIGINAL_RECORD_OUT_ID"),item.get("ORIGINAL_IN_VOUCHER_TYPE"),
					item.get("ORIGINAL_OUT_VOUCHER_TYPE"),item.get("EQUIPMENT_IN_ID"),item.get("EQUIPMENT_OUT_ID"),item.get("IN_TIME"),item.get("OUT_TIME"),item.get("DYNAMIC_BILLING_OUT_TIME"),
					item.get("MULTIPLE_CAR_TO_TEMP"),item.get("VOUCHER_TYPE"),item.get("VOUCHER_NO"),item.get("PLATE_COLOR"),item.get("IN_PHYSICAL_NO"),item.get("OUT_PHYSICAL_NO"),
					item.get("IN_IMG_ADDR"),item.get("OUT_IMG_ADDR"),item.get("STATUS"),item.get("IN_RECORD_TYPE"),item.get("WITHHOLD_FLAG"),item.get("WITHHOLD_MODE"),item.get("WITHHOLD_LIMIT"),
					item.get("WITHHOLD_BALANCE"),item.get("WITHHOLD_USERSTATUS"),item.get("WITHHOLD_CHANNEL"),item.get("IN_USER_ID"),item.get("IN_USER_NAME"),item.get("IN_PASS_ID"),
					item.get("IN_PASS_NAME"),item.get("IN_AREA_ID"),item.get("PARK_ID"),item.get("PROJECT_CODE"),item.get("IN_REMARK"),item.get("OUT_REMARK"),item.get("CAR_TYPE"),
					item.get("OUT_PASS_ID"),item.get("OUT_AREA_ID"),item.get("OUT_USER_ID"),item.get("OUT_RECORD_TYPE"),item.get("OLD_VOUCHER_NO"),item.get("RECORD_TYPE"),item.get("IN_EVENT_TYPE"),
					item.get("OUT_EVENT_TYPE"),item.get("IN_MEAL_ID"),item.get("OUT_MEAL_ID"),item.get("WITHHOLD_USER_ID"),item.get("OUT_TYPE"),item.get("PARTITION_CODE"),item.get("IS_LOCK"),
					item.get("LOCK_RECORD_ID"),item.get("SERVICE_ID"),item.get("SERVICE_NAME"),item.get("PERSON_ID"),item.get("PERSON_NAME"),item.get("person_name_cipher"),item.get("PERSON_PHONE"),
					item.get("person_phone_cipher"),item.get("PERSON_PHONE_LAST"),item.get("CAR_SERVICE_TYPE"),item.get("SYNC_REMARK"),item.get("SYNC_TIME"),item.get("SYNC_FLAG"),
					item.get("SYNC_FAILS"),item.get("IS_ONLINE_CALC"),item.get("ONLINECALCTYPE"),item.get("ONLINECALCTYPEDES"),item.get("OUTSOUNDTEXT"),item.get("OUTSCREENTEXT"),item.get("SOFTTEXT"),
					item.get("DELETE_FLAG"),item.get("PASS_MODE"),item.get("DATA_ORIGIN"),item.get("ORIGIN_ID"),item.get("STARTTIME"),item.get("ENDTIME"),item.get("STARTDATE"),item.get("ENDDATE"),
					item.get("IN_OPEN_GATE"),item.get("IN_OPEN_TIME"),item.get("OUT_OPEN_GATE"),item.get("OUT_OPEN_TIME"),item.get("SERVICE_CHANGE_TIME"),item.get("SERVICE_CHANGE_SIGN"),
					item.get("IN_OPEN_ORIGIN"),item.get("OUT_OPEN_ORIGIN"),item.get("ID"),dateOn);
			return true;
		}catch (Exception e) {
			e.printStackTrace();
			return false;
		}		
	}
	

	/**
	 * 同步出场日期
	 * @param fkeyId
	 * @return
	 */
	public synchronized Long  doAsyncOutTime(Long startId) {
		
		if(startId == null) startId = 1000l;
		 
		
		Long lastId = null;
		try {
			String sql = " select t.* ,rownum rn from ("
					+ "select id, record_id,out_time from  sharecenter.TP_PARK_RECORD where out_time is not null and id > ?  order by id asc  ) t "
					+ "where rownum < 2000  ";
			
			
			List<Map<String,Object>> list =  this.jdbcTemplateCarEnter.queryForList(sql,startId);
			for(Map<String,Object> map :list) {
				Long id = new Long(map.get("ID").toString());
				String recordId = (String) map.get("RECORD_ID");
				
				System.out.println(new Date()+", update date="+id+","+map.get("OUT_TIME")+",recordId="+recordId);
				
				//更新状态
				this.jdbcTemplateDataAi.update("update car_park_record set out_time = ? where form_id  = ?",map.get("OUT_TIME"),id);
				
				lastId = id;
			}
			
		}catch (Exception e) {
			e.printStackTrace();
			//更新报错，返回null
			return null;
		}
		
		return lastId;
		
	}
	

	/**
	 * 同步出场日期
	 * @param fkeyId
	 * @return
	 */
	public synchronized Long  doAsyncOutTimeFromTempTable(Long startId) {
		
		if(startId == null) startId = 0l;
		 
		
		Long lastId = null;
		try {
			String sql = " select * from temp_car_park where id > ?   order by id asc  limit 0,2000";
			
			
			List<Map<String,Object>> list =  this.jdbcTemplateDataAi.queryForList(sql,startId);
			for(Map<String,Object> map :list) {
				Long id = new Long(map.get("id").toString());
				
				//更新状态
				this.jdbcTemplateDataAi.update("update car_park_record set out_time = ? where voucher_no  = ? and in_time = ? ",
						map.get("out_time"),map.get("voucher_no"),map.get("in_time"));
				
				lastId = id;
			}
			
		}catch (Exception e) {
			e.printStackTrace();
			//更新报错，返回null
			return null;
		}
		
		return lastId;
		
	}
}
